package com.zsrt.javaweb;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PasswordFilter extends HttpFilter {

	@Override
	public void doHttpFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		System.out.println("进入PasswordFilter...");
		String password = request.getParameter("password");
		
		//拿到当前web应用的初始化参数
		String webPassword = getFilterConfig().getServletContext().getInitParameter("password");
		
		if(!password.equals(webPassword)){
			
			//放置错误提示消息
			request.setAttribute("message", "密码错误...");
			
			//如果password不等于"123456"就将请求转发到login.jsp
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			
			//表示不执行后面的方法 不交给下一个过滤器处理
			return;
		}
		chain.doFilter(request, response);
		// TODO Auto-generated method stub
		
	}


}
